package com.yx1996.test;

import com.yx1996.mapper.OrdersMapper;
import com.yx1996.pojo.Orders;
import com.yx1996.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.util.List;

public class testOrders {

    // 测试查询所有订单
    @Test
    public void fun() throws Exception {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();

        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);

        List<Orders> allOrsers = ordersMapper.findAllOrsers();

        for (Orders orsesr : allOrsers) {

            System.out.println(orsesr);
            System.out.println("");
        }
    }

    // 测试根据UID查询订单
    @Test
    public void fun1() throws Exception {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();

        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);

        User orserByUid = ordersMapper.findOrserByUid(1);

        System.out.println(orserByUid);
    }

    // 测试查询所有订单   订单一对一用户
    @Test
    public void fun2() throws Exception {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();

        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);

        List<Orders> allOrsers = ordersMapper.findOrsers();

        for (Orders orsesr : allOrsers) {

            System.out.println(orsesr);
            System.out.println("");
        }
    }

    // 测试查询所有订单   用户一对多订单
    @Test
    public void fun3() throws Exception {

        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
        // 获得session
        SqlSession sqlSession = sqlSessionFactory.openSession();

        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);

        List<User> users = ordersMapper.findUsers();

        for (User user : users) {

            System.out.println(user);
        }
    }
}
